/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuButton.java
 *
 * Created on 07.12.2008, 16:25:01
 */
package trim.gui.testing.components;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

/**
 *
 * @author Дмитрий
 */
public class MenuButton extends javax.swing.JPanel
{

  /** Creates new form MenuButton */
  public MenuButton()
  {
    initComponents();
    afterInitComponent();
  }

  private void afterInitComponent()
  {
    setLayout(new BorderLayout());
    this.add(btButton,java.awt.BorderLayout.CENTER);
    this.add(btMenu,java.awt.BorderLayout.EAST);

  }

  public void setText(String text)
  {
    btButton.setText(text);
  }

  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    popupMenu = new javax.swing.JPopupMenu();
    btButton = new javax.swing.JButton();
    btMenu = new javax.swing.JButton();

    btButton.setText("jButton1");

    btMenu.setText("V");
    btMenu.setMargin(new java.awt.Insets(2, 2, 2, 2));
    btMenu.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btMenuActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(btButton)
        .addGap(0, 0, 0)
        .addComponent(btMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(btButton)
        .addComponent(btMenu))
    );
  }// </editor-fold>//GEN-END:initComponents

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btMenuActionPerformed
    {//GEN-HEADEREND:event_btMenuActionPerformed
      popupMenu.show(btButton,0,getHeight());
    }//GEN-LAST:event_btMenuActionPerformed

  public void add(JMenuItem item)
  {
    popupMenu.add(item);
  }

  public void add(JSeparator separator)
  {
    popupMenu.add(separator);
  }

  public void addMenuButtonActionListener(ActionListener actionListener)
  {
    btMenu.addActionListener(actionListener);
  }

  public void addButtonActionListener(ActionListener actionListener)
  {
    btButton.addActionListener(actionListener);
  }

  public void clearMenu()
  {
    popupMenu.removeAll();
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btButton;
  private javax.swing.JButton btMenu;
  private javax.swing.JPopupMenu popupMenu;
  // End of variables declaration//GEN-END:variables
}
